﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;

namespace dhSystemBLL.orderData
{
   public  class stockListBLL
    {
       private stockListTableAdapter _stockListAdapter = null;
       private stockListTableAdapter stockListAdapter
        {
            get
            {
                if (_stockListAdapter == null) _stockListAdapter = new stockListTableAdapter();
                return _stockListAdapter;
            }
        }
        /// <summary>
       /// 查询仓库信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public orderDataSet.stockListDataTable getstockListAll()
        {
            try
            {
                return stockListAdapter.GetDatastockListAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 根据仓库名称查询仓库信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public orderDataSet.stockListDataTable getstockListAll(string stockName)
        {
            try
            {
                return stockListAdapter.GetDatastockListBystockName(stockName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 插入仓库信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int stockListInsert(stockListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                stockListDAL stockList = new stockListDAL(tn, conn);
                stockList.stockListInsert(row);
                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个仓库名称已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
               
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
       /// 更新仓库信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int stockListUpdate(stockListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                stockListDAL stockList = new stockListDAL(tn, conn);
                stockList.stockListUpdate(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
       /// 删除一行仓库信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int stockListDel(string stockName)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                stockListDAL stockList = new stockListDAL(tn, conn);
                stockList.stockListDel(stockName);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
